package cn.tedu.project.oa.asset.service.impl;

import cn.tedu.project.oa.asset.mapper.AssetMapper;
import cn.tedu.project.oa.asset.pojo.dto.AssetQuery;
import cn.tedu.project.oa.asset.pojo.dto.AssetSaveParam;
import cn.tedu.project.oa.asset.pojo.entity.Asset;
import cn.tedu.project.oa.asset.pojo.vo.AssetVO;
import cn.tedu.project.oa.asset.service.AssetService;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.vehicle.mapper.VehicleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional
@Service
@Slf4j
public class AssetServiceImpl implements AssetService {

    @Autowired
    AssetMapper assetMapper;
    @Autowired
    VehicleMapper  vehicleMapper;

    @Override
    public PageData selectAsset(AssetQuery assetQuery) {
        log.debug("查询固定资产列表业务:assetQuery={}",assetQuery);

        List<AssetVO> list = assetMapper.selectAsset(assetQuery);

        PageData pageData = new PageData();
        pageData.setList(list);

        int num = assetMapper.selectCount(assetQuery);

        pageData.setTotal(num);

        return pageData;
    }

    @Override
    public void saveAsset(AssetSaveParam assetSaveParam) {
        log.debug("保存固定资产业务:assetSaveParam={}",assetSaveParam);

        Asset asset = new Asset();
        BeanUtils.copyProperties(assetSaveParam,asset);
        if(asset.getId() == null){
            asset.setCreateTime(new Date());
            assetMapper.saveAsset(asset);
        }else {
            asset.setUpdateTime(new Date());
            assetMapper.updateAsset(asset);
        }
    }

    @Override
    public void deleteAsset(Long id,String code) {
        log.debug("删除固定资产业务:id={},assetCode={}",id,code);

        assetMapper.deleteById(id);


        vehicleMapper.deleteByAssetCode(code);

    }

    @Override
    public void updateAssetByAssetCode(AssetSaveParam assetSaveParam) {
        log.debug("更新车辆资产业务:assetSaveParam={}",assetSaveParam);

        assetSaveParam.setUpdateTime(new Date());
        assetMapper.updateAssetByAssetCode(assetSaveParam);
    }

    @Override
    public void deleteByAssetCode(String assetCode) {
        log.debug("删除车辆资产业务:assetCode={}",assetCode);

        String code = assetCode;
        assetMapper.deleteByAssetCode(code);
    }

    @Override
    public void updateStockNum(Long id, Long checkNum) {
        log.debug("更新盘点数业务:id={},checkNum={}",id,checkNum);

        Asset asset = new Asset();
        asset.setId(id);
        asset.setStockNum(checkNum);
        asset.setUpdateTime(new Date());

        assetMapper.updateAsset(asset);
    }

    @Override
    public int count() {
        log.debug("展示目前资产数量业务");

        int num = assetMapper.count();

        return num;
    }
}
